//
//  SignalCollectionView.m
//  Select
//
//  Created by dihuijun on 17/3/20.
//  Copyright © 2017年 Cycle. All rights reserved.
//

#import "SignalCollectionView.h"
#import "CollectionChooseViewCell.h"

#define ItemHeight 70
#define HeaderHeight 50

static NSString *CellID = @"aCellID";
static NSString *HeaderID = @"aHeaderID";

@implementation SignalCollectionView

/**  初始化*/
+ (instancetype)ShareSignalCollectionWithFrame:(CGRect)frame{
    SignalCollectionView *signalCollection = [[SignalCollectionView alloc]initWithCollectionViewFrame:frame];
    return signalCollection;
}
- (instancetype)initWithCollectionViewFrame:(CGRect)frame{
    self = [super init];
    if (self) {
        self.frame = frame;
        
        //创建子控件
        [self setupCollectionView];
    }
    return self;
}
#pragma mark ---- 创建子控件
- (void)setupCollectionView
{
    //摆布方式
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    //列距离
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 0;
    
    //1.创建collectionView
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.frame.size.height) collectionViewLayout:layout];
    //背景颜色
    self.collectionView.backgroundColor = ColorRGB(0xf7f7f7);
    //注册cell
    [self.collectionView registerClass:[CollectionChooseViewCell class] forCellWithReuseIdentifier:CellID];
    //不实现竖着的滚动条
    self.collectionView.showsVerticalScrollIndicator = NO;
    //可以滚动
    self.collectionView.scrollEnabled = YES;
    // 代理
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self addSubview:self.collectionView];
    
    
}
#pragma mark --- <UICollectionViewDelegate,UICollectionViewDataSource>
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.datasource.count;
}
//元素大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(SCREEN_WIDTH/3, ItemHeight);
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CollectionChooseViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellID forIndexPath:indexPath];
    
    //背景颜色
    cell.backgroundColor = [UIColor clearColor];
    //负值
    cell.titleLabel.text = self.datasource[indexPath.row];
    NSLog(@"%zd",cell.isSelected);
    
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    //判断第二次点击的时候，我们记录的currentIndexPAth是否记录了第一次的值，且是否等于第二次点击的时候的indexPath，不等于就把第一次的按钮状态改回来
    if (_currentIndexPath !=nil && _currentIndexPath != indexPath) {
        
        CollectionChooseViewCell *cell = (CollectionChooseViewCell *)[collectionView cellForItemAtIndexPath:_currentIndexPath];
        
        [cell updateCellSelectedState:NO];
        
    }else{
        NSLog(@"....");
    }
    
    CollectionChooseViewCell *cell = (CollectionChooseViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    [cell updateCellSelectedState:!cell.isSelected];
    
    self.currentIndexPath = indexPath;
    _block(cell.titleLabel.text,indexPath);
    
    
    
    
    
    
    
    
    
    
    
    
    
}
/** 刷新表格*/
- (void)reloadData{
    [self.collectionView reloadData];
}

@end
